//
//  DataBaseManager.m
//  WCDB_Demo
//
//  Created by lxj on 2018/7/25.
//  Copyright © 2018年 lxj. All rights reserved.
//

#import "DataBaseManager.h"
#import <WCDB/WCDB.h>
@interface DataBaseManager()

@property(nonatomic ,strong) WCTDatabase *database;

@end



@implementation DataBaseManager

+ (DataBaseManager *)sharedInstance {
    static DataBaseManager *sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[self alloc] init];
    });
    return sharedInstance;
}

- (id)dataBase{
    return self.database;
}

///创建数据库
- (void)createDataBase{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docDir = [paths objectAtIndex:0];
    NSString *path = [NSString stringWithFormat:@"%@.db",[docDir stringByAppendingPathComponent:@"lxj"]];

    self.database = [[WCTDatabase alloc] initWithPath:path];
    self.database.tag = 1;
    [self createTable];
//    [self createFTS];
}
///创建表
- (void)createTable{
    BOOL result = [self.database createTableAndIndexesOfName:@"Message" withClass:TABLE_WCDB_CLASS(@"Message")];
}
///创建FTS
- (void)createFTS{
    ///注册分词器
    [self.database setTokenizer:WCTTokenizerNameWCDB];
    //接口创建FTS表
    BOOL result = [self.database createVirtualTableOfName:@"MessageFTS" withClass:TABLE_WCDB_CLASS(@"Message")];
}
- (void)insertObject:(WCTObject *)object into:(NSString *)tableName{
    object.isAutoIncrement = YES;
    [self.database insertObject:object into:tableName];
}
- (NSArray *)getAllObjectsOfClass:(Class)className tableName:(NSString *)tableName{
    return [self.database getAllObjectsOfClass:className fromTable:tableName];
}
- (BOOL)deleteAllObjectsFromTable:(NSString *)tableName{
    return [self.database deleteAllObjectsFromTable:tableName];
}
@end
